package com.group.app.example.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    private static SimpleDateFormat getSimpleDateFormat(){
        String pattern="yyyy-MM-dd";
        return new SimpleDateFormat(pattern);
    }

    public static Date getTodayDate(){
        Calendar calendar=Calendar.getInstance();
        return calendar.getTime();
    }

    public static String getTodayString(){
        Calendar calendar=Calendar.getInstance();
        return  DateUtil.getSimpleDateFormat().format(calendar.getTime());
    }

    public static Timestamp getCurrentTime(){
        Calendar calendar=Calendar.getInstance();
        return  new Timestamp(calendar.getTimeInMillis());
    }


    public static Date getDateFromString(String string) throws ParseException{
        return DateUtil.getSimpleDateFormat().parse(string);
    }

    private  static  String addDate(int days){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,days);
        return  DateUtil.getSimpleDateFormat().format(calendar.getTime());
    }

    /*
      获取指定年月的最后一天日期
     */
    public static Date getLastDayOfMonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,year);
        cal.set(Calendar.MONTH,month-1);
        int lastDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH,lastDay);
        return cal.getTime();
    }

    /*
       获取去年每月月底日期
     */
    public static List<Date> getLastDayOfMonthLastYear(Date date){
        List list=new ArrayList();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int lastYear=calendar.get(Calendar.YEAR) - 1;
        for(int i =1;i<=12;i++){
            Date lastDate=getLastDayOfMonth(lastYear,i);
            list.add(lastDate);
        }
        return  list;
    }

    /*
       获取去年每月月底日期
     */
    public static List<Date> getLastDayOfMonthThisYear(Date date){
        List list=new ArrayList();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int lastYear=calendar.get(Calendar.YEAR);
        for(int i =1;i<=calendar.get(Calendar.MONTH)+1;i++){
            Date lastDate=getLastDayOfMonth(lastYear,i);
            list.add(lastDate);
        }
        list.add(date);
        return  list;
    }

    /*
           获取某年某周的最后一天
     */
    public static Date getLastDayOfWeek(int year,int week){
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.WEEK_OF_YEAR,week);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek()+6);
        return calendar.getTime();
    }

    /**
     * 获取指定日期近7周的每周最后一天的日期
     * @param date
     * @return
     */
    public static List<Date> getLast7WeekList(Date date){
        List list=new ArrayList();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int isYear = calendar.get(Calendar.YEAR);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        list.add(date);
        for(int i=1;i<=6;i++){
            calendar.add(Calendar.WEEK_OF_YEAR,-i);
            Date lastDayOfWeek =null;
            if(calendar.get(Calendar.WEEK_OF_YEAR)==1 && calendar.get(Calendar.YEAR) == isYear -1 ){
                int year =calendar.get(Calendar.YEAR)+1;
                lastDayOfWeek=getLastDayOfWeek(year,calendar.get(Calendar.WEEK_OF_YEAR));
            }else{
                lastDayOfWeek=getLastDayOfWeek(calendar.get(Calendar.YEAR),calendar.get(Calendar.WEEK_OF_YEAR));
            }
            calendar.add(Calendar.WEEK_OF_YEAR,i);
            list.add(lastDayOfWeek);
        }
        return list;
    }


    /**
     * 获取上周第一天至最后一天的日期列表
     * @param date
     * @return
     */
    public static  List<Date> getLastWeekOfDayList(Date date){
        List list=new ArrayList();
        Calendar startCal=Calendar.getInstance();
        startCal.setTime(date);
        startCal.setFirstDayOfWeek(Calendar.MONDAY);
        startCal.add(Calendar.DATE,-1 * 7);
        startCal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        Calendar endCal=Calendar.getInstance();
        endCal.setTime(date);
        endCal.add(Calendar.DATE, 7);
        while (startCal.before(endCal)){
            list.add(startCal.getTime());
            startCal.add(Calendar.DAY_OF_YEAR,1);
        }
        return  list;
    }

    /**
     * 获取本周第一天至当天的日期列表
     * @param date
     * @return
     */
    public static  List<Date> getThisWeekOfDayList(Date date){
        List list=new ArrayList();
        Calendar startCal=Calendar.getInstance();
        startCal.setTime(date);
        startCal.setFirstDayOfWeek(Calendar.MONDAY);
        startCal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        Calendar endCal=Calendar.getInstance();
        endCal.setTime(date);
        while (startCal.before(endCal)){
            list.add(startCal.getTime());
            startCal.add(Calendar.DAY_OF_YEAR,1);
        }
        list.add(date);
        return  list;
    }

}
